package com.chixing.commons;


import com.chixing.pojo.Likes;
import com.chixing.service.ILikesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class StaticScheduleTask {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ILikesService likesService;

    @Scheduled(fixedRate = 5000)
    public void configureTasks(){
        List<Likes> likesList = new ArrayList<>();
//        likesList.add((Likes)(redisTemplate.opsForList().index("likes*",-1)));
        List<Object> lists = new ArrayList<>();
        Set<String> set = new HashSet<>();
        set.addAll(redisTemplate.keys("likes*"));
        String[] keynames = new String[set.size()];
        String[] strings= set.toArray(keynames);
        System.out.println("长度："+strings.length);
        for (int i = 0;i< strings.length;i++)
        {
            String key = strings[i];
//            System.out.println("key:"+key);
            List<Object> list = new ArrayList<>();

            list=redisTemplate.boundListOps(key).range(0,-1);
            redisTemplate.delete(key);
            lists.addAll(list);
            /*for (int j=0;j< list.size();j++)
                lists.add(list.get(j));*/
//            System.out.println(redisTemplate.boundListOps(key).range(0,-1));
        }
        System.out.println("likes:");
        for (int i=0;i< lists.size();i++){
            Likes likes = (Likes) lists.get(i);
            likesService.addlikes((Likes) lists.get(i));
            System.out.println(likes);
        }
        System.out.println("likessize:"+lists.size());

    }

}
